package com.felix.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.felix.bean.Temp_mess_list;
import com.felix.bean.User;
import com.felix.dao.EditDao;
import com.g3net.database.DataBase;
import com.g3net.database.DataBaseFactory;
import com.g3net.database.DbException;

public class EditDaoImpl implements EditDao {

	public int updateUserInfo(User u) throws DbException {
		DataBase db= DataBaseFactory.getDataBase();
		return db.excuteUpdateClass(u, "id");
	}

	public int updateTempList(Temp_mess_list temp) throws Exception {
		// TODO Auto-generated method stub
		String sql=null;
		if (temp.getUnread()==-1) {
			sql="update temp_mess_list set messagecount=? ,unread=unread+1 where uid = ? and pid = ? and cid = ?";
		}else if (temp.getUnread()==-2) {
			sql="update temp_mess_list set messagecount=? where uid = ? and pid = ? and cid = ?";
		}
		else {
			 sql="update temp_mess_list set messagecount=? ,unread=? where uid = ? and pid = ? and cid = ?";
		}
		DataBase db= DataBaseFactory.getDataBase();
		Connection conn = null;
		PreparedStatement ptmt = null;
		
		conn=db.getConnection();
		try {
			int i=1;
			ptmt=conn.prepareStatement(sql);
			ptmt.setInt(i, temp.getMessagecount());
			if (temp.getUnread()>-1) {
				i++;
				ptmt.setInt(i, temp.getUnread());
			}
			i++;
			ptmt.setInt(i, temp.getUid());
			i++;
			ptmt.setInt(i, temp.getPid());
			i++;
			ptmt.setInt(i, temp.getCid());
			ptmt.addBatch();
			ptmt.execute();
			
			ptmt.close();
			conn.close();
			db.close();
		} catch (SQLException e) {
			if (ptmt!=null) {
				ptmt.close();
			}
			if (conn!=null) {
				conn.close();
			}
			if (db!=null) {
				db.close();
			}
		}
		return 0;
	}

	public int updateLeftSit(int cid, int left) throws Exception {
		// TODO Auto-generated method stub
		String sql="update carshare set sitleft= ? where cid = ?";
		DataBase db= DataBaseFactory.getDataBase();
		Connection conn = null;
		PreparedStatement ptmt = null;
		
		conn=db.getConnection();
		try {
			ptmt=conn.prepareStatement(sql);
			ptmt.setInt(1, left);
			ptmt.setInt(2, cid);
			ptmt.addBatch();
			ptmt.execute();
			ptmt.close();
			conn.close();
			db.close();
		} catch (SQLException e) {
			if (ptmt!=null) {
				ptmt.close();
			}
			if (conn!=null) {
				conn.close();
			}
			if (db!=null) {
				db.close();
			}
		}
		return 0;
	}

	public int updateTempList2(Temp_mess_list temp) throws Exception {
		// TODO Auto-generated method stub
		String sql="update temp_mess_list set unread=0 where uid = ? and pid = ? and cid = ?";
	
		DataBase db= DataBaseFactory.getDataBase();
		Connection conn = null;
		PreparedStatement ptmt = null;
		
		conn=db.getConnection();
		try {
			ptmt=conn.prepareStatement(sql);
			ptmt.setInt(1, temp.getUid());
			ptmt.setInt(2, temp.getPid());
			ptmt.setInt(3, temp.getCid());
			ptmt.addBatch();
			ptmt.execute();
			
			ptmt.close();
			conn.close();
			db.close();
		} catch (SQLException e) {
			if (ptmt!=null) {
				ptmt.close();
			}
			if (conn!=null) {
				conn.close();
			}
			if (db!=null) {
				db.close();
			}
		}
		return 0;
	}

	public int updatePassword(String mobi, String pwd) throws Exception {
		// TODO Auto-generated method stub
		String sql="update user set password = ? where mobile = ?";
		DataBase db= DataBaseFactory.getDataBase();
		Connection conn = null;
		PreparedStatement ptmt = null;
		
		conn=db.getConnection();
		try {
			ptmt=conn.prepareStatement(sql);
			ptmt.setString(1, pwd);
			ptmt.setString(2, mobi);
			ptmt.addBatch();
			ptmt.executeUpdate();
			
			ptmt.close();
			conn.close();
			db.close();
		} catch (SQLException e) {
			if (ptmt!=null) {
				ptmt.close();
			}
			if (conn!=null) {
				conn.close();
			}
			if (db!=null) {
				db.close();
			}
		}
		return 0;
	}

}
